<?
/*
 * Copyright 2004-2005 Sigve Indregard.
 *
 * This file is part of Laivsys.
 *
 * Laivsys is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Laivsys is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Laivsys; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * $Id: bilder.php 6 2005-07-11 23:14:47Z say $
 */
 
include("../db.php");
include("auth.php");
include("top.php");
include("rollerinclude.php");
echo "<h1>Bilder</h1>\n";

$proc=$_REQUEST['process'];

$debug=false;

function debug ($text) {
	global $debug;

	if ($debug) 
		echo "$text<br/>\n";
}

if ($proc == 'chgbesk') {
	$besk=mysql_escape_string($_REQUEST['beskrivelse']);
	$id=mysql_escape_string($_REQUEST['id']);

	$sql="UPDATE Bilder SET Beskrivelse='$besk' WHERE ID=$id";
	mysql_query($sql);

	uploadForm();	
} elseif ($proc == 'del') {
	$id=mysql_escape_string($_REQUEST['id']);

	$sql="DELETE FROM Bilder WHERE ID=$id AND LaivID=$laivid";
	mysql_query($sql);
} elseif ($proc == 'edit') {
	$id=mysql_escape_string($_REQUEST['id']);
	printChgBesk($id);	
} elseif ($proc == 'add') {
	$fil = "/home/wolfraidah/urvno/htdocs/arrangementer/cms/admin/php_gd2.dll";
	$x= preg_replace('/\/([^\/]+)/', '../', dirname($fil)) . $fil;
	dl($x);

	debug("Starting adding process");
	$fil = $_FILES['fil']['tmp_name'];
	debug("Filename: $fil");
	$type=$_FILES['fil']['type'];
	debug("MIME: $type");

	if ($type == 'image/jpeg' || $type=="image/pjpeg") {
		debug("It's a jpeg!");
		$filetype="jpeg";
		$im = imagecreatefromjpeg($fil);
		debug("Created image!");
	} elseif ($type == 'image/gif') {
		$filetype="gif";
		$im = imagecreatefromgif($fil);
	} elseif ($type == 'image/png') {
		$filetype="png";
		$im = imagecreatefrompng($fil);
	}

	debug("Filetype: $filetype");
	
	//try to open the picture as a gd image
	if (!$im) {
		echo "Forstod ikke filen. Den er enten ikke et bilde av jpeg-, gif- eller png-format, eller s� er den skadet.";
	} else {
		//do not save larger files than this
		$maxW=640;
		$maxH=480;
		$stdRatio = $maxW/$maxH;

		debug("Standard: $maxW x $maxH; ratio $stdRatio");

		$w=imagesx($im);
		$h=imagesy($im);
		$ratio = $w/$h;

		debug("Image: $w x $h; ratio $ratio");

		if ($w > $maxW || $h > $maxH) {
			//we need to resize.
			debug("Resize in progress");
			if ($ratio <= $stdRatio) {
				//height is most off
				$newH=$maxH;
				//keep the ratio!
				$newW=(int)($w*($maxH/$h));
			} else {
				//width is most off
				$newW=$maxW;
				//keep the ratio!
				$newH=(int)($h*($maxW/$w));
			}

			debug("New size: $newW x $newH");

			$dest_im = imagecreatetruecolor($newW, $newH);
			imagecopyresampled($dest_im, $im, 0, 0, 0, 0, $newW, $newH, $w, $h);

			debug("Copied the image...");

			$im = $dest_im;

			$w = $newW;
			$h = $newH;
		}
		//ok, do some fixing on the image
		$background=imagecolorallocate($im, 255, 255, 255);
		$black=imagecolorallocate($im, 0, 0, 0);

		//draw a frame
		imagesetthickness($im, 2);
		imageline($im, 0, 1, $w, 1, $black);
		imageline($im, 0, $h-1, $w, $h-1, $black);
		imageline($im, 1, 0, 1, $h, $black);
		imageline($im, $w-1, 0, $w-1, $h, $black);
		debug("Frame drawn");

		//print copyright info
		imagestring($im, 1, 3, 3, "(c) URV", $background);

		//flush image as png to a buffer
		ob_start();
		@imagepng($im);
		$im_data = ob_get_contents();
		ob_end_clean();

		//make our thumbnail (a random cut)
		$thumb_w=100;
		$thumb_h=100;

		if ($w <= $thumb_w) {
			$thumb_startx = 1;
			$thumb_w = $w;
		} else {
			//from 0 to (w-thumb_w)
			$thumb_startx = rand(1, $w-$thumb_w);
		}
		if ($h <= $thumb_h) {
			$thumb_starty = 1;
			$thumb_h = $h;
		} else {
			//from 0 to (h-thumb_h)
			$thumb_starty = rand(1, $h-$thumb_h);
		}

		$thumb_endx = $thumb_startx + $thumb_w;
		$thumb_endy = $thumb_starty + $thumb_h;

		$thumb = imagecreatetruecolor($thumb_w, $thumb_h);
		imagecopy($thumb, $im, 0, 0, $thumb_startx, $thumb_starty, $thumb_endx, $thumb_endy);

		//flush thumb to a buffer
		ob_start();
		imagepng($thumb);
		$thumb = ob_get_contents();
		ob_end_clean();

		//cleaning up is very necessary
		imagedestroy($image);
		imagedestroy($thumb);
		imagedestroy($dest_im);
	
		$im_data=mysql_escape_string($im_data);
		$thumb=mysql_escape_string($thumb);
		$sql = "INSERT INTO Bilder (LaivID, Beskrivelse, Datatype, Bilde, Bilde_w, Bilde_h, Thumb, Thumb_w, Thumb_h) ";
		$sql.= "VALUES ($laivid, '', 'image/png', '$im_data', $w, $h, '$thumb', $thumb_w, $thumb_h)";
		mysql_query($sql) or die(mysql_error()."<div style=\"border:1px solid black; background:white; margin:20px;\">$sql</div>");

		$newid=mysql_insert_id();

		printChgBesk($newid);

		unlink($fil);
	}
} else {
	uploadForm();
}

function uploadForm() {
	global $laivid;
	echo "<h2>Last opp bilde</h2>\n";
	?>
	<table class="gridded">
	<tr><td>
	<form method="post" action="bilder.php" enctype="multipart/form-data">
		<input type="hidden" name="process" value="add"/>
		<p><b>Fil:</b><br/>
		<input type="file" name="fil"></p>

		<p><input type="submit" value="Last opp"></p>
	</form>
	</td></tr>
	</table>
	<?
	echo "<h2>Bilder i databasen</h2>\n";
	$sql = "SELECT ID, Beskrivelse FROM Bilder WHERE LaivID=$laivid ORDER BY ID";
	$bilder = mysql_query($sql);

	echo "<table class=\"gridded\">\n";
	$i=1;
	while ($bilde = mysql_fetch_assoc($bilder)) {
		echo "<tr>\n";
		echo "<td>$i</td>\n";
		echo "<td>".nl2br($bilde['Beskrivelse'])."</td>\n";
		echo "<td><a href=\"bilder.php?process=edit&id={$bilde['ID']}\">[rediger]</a></td>\n";
		echo "<td><a href=\"bilder.php?process=del&id={$bilde['ID']}\">[slett]</a></td>\n";
		echo "</tr>\n";
		$i++;
	}
	echo "</table>\n";
}
include("bottom.php");


function printChgBesk($id) {
	global $laivid;
	$sql = "SELECT * FROM Bilder WHERE ID=$id AND LaivID=$laivid";
	$bilder=mysql_query($sql);

	if (mysql_num_rows($bilder)) {
		$bildet=mysql_fetch_assoc($bilder);
		
		echo "<h2>Endre beskrivelse</h2>";
		echo "<p><img src=\"bilde_vis.php?id=$id\" width=\"{$bildet['Bilde_w']}\" height=\"{$bildet['Bilde_h']}\" alt=\"{$bildet['Beskrivelse']}\"/></p>";
		?>
		<form method="post" action="bilder.php">
		<input type="hidden" name="process" value="chgbesk"/>
		<input type="hidden" name="id" value="<?=$id?>"/>
		<p>
		<textarea name="beskrivelse" cols="60" rows="5"><?=$bildet['Beskrivelse']?></textarea></p>
		<p><input type="submit" value="Lagre"/></p>
		</form>
		<?
	}
}
?>
